package com.fjwt.gz.mgr.ctrl.fans;

import cn.hutool.core.util.DesensitizedUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.components.mq.model.ExportExcelMQ;
import com.fjwt.gz.components.mq.vender.IMQSender;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.core.entity.SysUser;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.core.model.security.GzUserDetails;
import com.fjwt.gz.db.dto.OldFansInfoDTO;
import com.fjwt.gz.db.dto.StatMemberInfoDTO;
import com.fjwt.gz.db.entity.StatMemberInfoEntity;
import com.fjwt.gz.mgr.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.OldFansInfoService;
import com.fjwt.gz.service.impl.StatMemberInfoService;
import com.fjwt.gz.service.impl.SysExportTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.List;

/**
 * 存量会员管理
 */
@Slf4j
@RestController
@RequestMapping("/api/oldFans")
public class OldFansAndExportController extends CommonCtrl {

    @Autowired
    private OldFansInfoService oldFansInfoService;

    @Autowired
    private StatMemberInfoService memberInfoService;

    @Autowired
    private IMQSender mqSender;
    @Autowired
    private SysExportTaskService sysExportTaskService;

    /**
     * 存量用户查询
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_DATA_REPORT_MEMBER_OLD')")
    @PostMapping("/getList")
    public ApiRes getList() {
        LocalDate date = LocalDate.now();
        LocalDate endDate = date.minusWeeks(1).with(DayOfWeek.SUNDAY);
        OldFansInfoDTO dto = getObject(OldFansInfoDTO.class);
        dto.setSyncDate(endDate.toString());
        IPage<OldFansInfoDTO> page = oldFansInfoService.selectOldFansInfoByDTO(getIPage(),dto);
        // 数据脱敏
        page.getRecords().forEach(i -> {
            i.setPhone(DesensitizedUtil.mobilePhone(i.getPhone()));
        });
        return ApiRes.page(page);
    }


    /**
     * 导出存量用户报表
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_DATA_REPORT_MEMBER_OLD_EXPT')")
    @PostMapping("/exportOldFansInfo")
    public ApiRes exportOldFansInfo(){
        SysUser sysUser = getCurrentUser().getSysUser();
        LocalDate date = LocalDate.now();
        LocalDate endDate = date.minusWeeks(1).with(DayOfWeek.SUNDAY);
        OldFansInfoDTO dto = getObject(OldFansInfoDTO.class);
        dto.setSyncDate(endDate.toString());
        // 添加导出任务记录
        Long exportTaskId = sysExportTaskService.addExportTask(sysUser,"存量用户报表",Constants.EXPORT_TASK_ENUM.REQUEST_CODE.OLD_FANS_INFO,
                Constants.EXPORT_TASK_ENUM.RESOURCE_TYPE.FANS,
                JSONObject.toJSONString(dto));
        // 发送mq
        mqSender.send(ExportExcelMQ.build(exportTaskId));
        return ApiRes.ok();
    }

    /**
     * 全量会员
     */
    @PreAuthorize("hasAnyAuthority('ENT_DATA_REPORT_MEMBER_ALL')")
    @PostMapping("/allMemberInfo")
    public ApiRes  allFansInfo(){
        StatMemberInfoDTO memberInfoDTO = getObject(StatMemberInfoDTO.class);
        LocalDate date = LocalDate.now();
        LocalDate endDate = date.minusWeeks(1).with(DayOfWeek.SUNDAY);
        memberInfoDTO.setLastWeekDate(endDate.toString());
        IPage<StatMemberInfoEntity> page = memberInfoService.allMemberInfo(getIPage(), memberInfoDTO);
        return ApiRes.page(page);
    }

    /**
     * 全量用户报表
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_DATA_REPORT_MEMBER_ALL_EXPT')")
    @PostMapping("/exportMemberInfo")
    public ApiRes exportMemberInfo(){
        SysUser sysUser = getCurrentUser().getSysUser();
        StatMemberInfoDTO memberInfoDTO = getObject(StatMemberInfoDTO.class);
        LocalDate date = LocalDate.now();
        LocalDate endDate = date.minusWeeks(1).with(DayOfWeek.SUNDAY);
        memberInfoDTO.setLastWeekDate(endDate.toString());
        // 添加导出任务记录
        Long exportTaskId = sysExportTaskService.addExportTask(sysUser,"全量用户报表",Constants.EXPORT_TASK_ENUM.REQUEST_CODE.MEMBER_INFO,
                Constants.EXPORT_TASK_ENUM.RESOURCE_TYPE.FANS,
                JSONObject.toJSONString(memberInfoDTO));
        // 发送mq
        mqSender.send(ExportExcelMQ.build(exportTaskId));
        return ApiRes.ok();
    }

}
